/**
 * 选择头像
 * @param el
 */
function uploadChangeHead(el){
	var file = $(el);
	$("#uploadTipsDiv").html("");
	$("#uploadTipsDefDiv").hide();
	var imgValue = file.val();
	if(imgValue != ''){
		 var ext=imgValue.substring(imgValue.lastIndexOf(".")+1).toLowerCase();

		 if(ext!='png'&&ext!='jpg'&&ext!='jpeg'){
	    	$("#uploadTipsDiv").html("仅支持jpg,jpeg,png图片文件");
			 return;
		 }

		 $("#uploadButtonDiv").hide();
		 $("#uploadingDiv").show();

		 var options = {
				 	dataType:'json',
				    success: function(data) {
				    	if(data.state == -1){
				    		$("#uploadTipsDiv").html("图片太大。(请限制在1M以内)");
				    	}else if(data.state == -2){
				    		$("#uploadTipsDiv").html("上传失败，请稍后再试");
					    }else{
							 myjcropdestroy();
				    		 $('.jcrop-preview').attr('src',data.url);
				    		 Jcrop.html("<img class='bigpic' src='" + data.url + "' onload='setImgPosition(\""+data.swidth+"\",\""+data.sheight+"\")' />");
				    		 $("#headImgSrc","#head_upload_form").val(data.url);

				    		 //确定上传按钮显示
							 $("#submitDiv").show();

							 //提示
							 $("#uploadTipsDiv").html("提示：请截取方形头像");
							 $("#uploadTipsDiv").attr("color","green");
				    	}
				    	//上传按钮显示
						 $("#uploadButtonDiv").show();
						 //上传加载中gif隐藏
						 $("#uploadingDiv").hide();
				    }
				};
		$("#head_upload_form").attr('action',$('#load_upload_head_div').attr("temurl"));
		$("#head_upload_form").ajaxSubmit(options);
	 }
}

//设置图片位置
function setImgPosition(swidth,sheigth){
	var oImg = $(".bigpic");//头像原图(但宽高已定死)

	if(swidth == undefined )
		swidth =  $("#my_true_head_img").attr("swidth");
	if(sheigth == undefined )
		sheigth =  $("#my_true_head_img").attr("sheight");

	var width1 = parseInt(swidth);
	var height1 = parseInt(sheigth);

	//var width2 = $(".jcrop-holder").width();
	//var height2 = $(".jcrop-holder").height();

	var width = 306;
	var height = 306;

	if(height1>width1){
		var big_h = width1*height;
		width = big_h/height1;
		//var dw=(width2-width)/2;
		//Jcrop.css("margin-left",dw+"px");
	}else if(height1<width1){
		var big_w = height1*width;
		height = big_w/width1;
		//var dh=(height2-height)/2;
		//Jcrop.css("margin-top",dh+"px");
	}

	oImg.css('width',width+"px");
	oImg.css('height',height+"px");
	$("#target").css('width',width+"px");
	$("#target").css('height',height+"px");
	$("#zw","#head_upload_form").val(width);
	$("#zh","#head_upload_form").val(height);
	$("#sw","#head_upload_form").val(width1);
	$("#sh","#head_upload_form").val(height1);
	Jcropinit();
}

//图片剪裁插件调用
var Jcrop;
function Jcropinit(){
    // Create variables (in this scope) to hold the API and image size
    var jcrop_api,
        boundx,
        boundy,

        // Grab some information about the preview pane
        $preview = $('#preview-pane'),
        $pcnt = $('#preview-pane .preview-container'),
        $pimg = $('#preview-pane .preview-container img'),

        xsize = $pcnt.width(),
        ysize = $pcnt.height();

    Jcrop = $('#target').Jcrop({
      onChange: updatePreview,
      onSelect: updatePreview,
      onRelease:  clearCoords,
      aspectRatio: xsize / ysize
    },function(){
      // Use the API to get the real image size
      var bounds = this.getBounds();
      boundx = bounds[0];
      boundy = bounds[1];
      // Store the API in the jcrop_api variable
      jcrop_api = this;

      // Move the preview into the jcrop container for css positioning
      $preview.appendTo(jcrop_api.ui.holder);
    });

    /**
     * 小窗口预览和huoq坐标
     */
    function updatePreview(c)
    {
      if (parseInt(c.w) > 0)
      {
        var rx = xsize / c.w;
        var ry = ysize / c.h;

        $pimg.css({
          width: Math.round(rx * boundx) + 'px',
          height: Math.round(ry * boundy) + 'px',
          marginLeft: '-' + Math.round(rx * c.x) + 'px',
          marginTop: '-' + Math.round(ry * c.y) + 'px'
        });

        $('#x1','#head_upload_form').val(c.x);
        $('#y1','#head_upload_form').val(c.y);
        $('#x2','#head_upload_form').val(c.x2);
        $('#y2','#head_upload_form').val(c.y2);
        $('#w','#head_upload_form').val(c.w);
        $('#h','#head_upload_form').val(c.h);
      }
    };

    function clearCoords(){
	  $('#head_upload_form input').val('');
	};

}

/**
 * 上传头像
 */
function uploadTrueHead(){
			var x = parseInt($('#x1','#head_upload_form').val());
		    var y = parseInt($('#y1','#head_upload_form').val());
		    var w = parseInt($('#w','#head_upload_form').val());
		    var h = parseInt($('#h','#head_upload_form').val());
		    var sw = parseInt($('#sw','#head_upload_form').val());
		    var sh = parseInt($('#sh','#head_upload_form').val());
		    var zw = parseInt($('#zw','#head_upload_form').val());
		    var zh = parseInt($('#zh','#head_upload_form').val());

//		    if(isNaN(x)||isNaN(y)||isNaN(w)||isNaN(h)){
//		    	$("#uploadTipsDiv").html("请截取头像");
//		    	return;
//		    }
		    if(isNaN(x))x=0;
		    if(isNaN(y))y=0;
		    if(isNaN(w))w=0;
		    if(isNaN(h))h=0;

		    w = parseInt((sw * w)/zw);
		    h = parseInt((sh * h)/zh);
		    x = parseInt((sw * x)/zw);
		    y = parseInt((sh * y)/zh);

			$('#x1','#head_upload_form').val(x);
		    $('#y1','#head_upload_form').val(y);
		    $('#w','#head_upload_form').val(w);
		    $('#h','#head_upload_form').val(h);

			var options = {
			 		dataType:'json',
				    success: function(data) {
				    	if(data.STATE==200){
				    			if($('#load_upload_head_div').attr("tag")=='salers'){
				    				//更新顶部头像
				    				$('#top_head_image').attr('src',data.imageInfo.url);
				    			}
								//更新当前图片
								$('#my_true_head_img').attr('src',data.imageInfo.url);
								$('#my_true_head_img').attr('swidth',data.imageInfo.swidth);
								$('#my_true_head_img').attr('sheight',data.imageInfo.sheight);

								//标记
								$("#headImageInput").val("1");
								//首页强行弹框头像上传提示语
								$("#tipsDiv").hide();
								$.modal.close();
				    	}else{
					    	$("#uploadTipsDiv").html("上传失败，请稍后再试！");
				    	}
				    }
			};

			$("#head_upload_form").attr('action',$('#load_upload_head_div').attr("realurl"));
			$("#head_upload_form").ajaxSubmit(options);
}

/**
 * 打开上传头像页面
 */
function openUploadHead(){
	$('#load_upload_head_div').load('/account/editavatar');
	$("#uploadTipsDiv").attr("color","red");
	$('.box').modal();
}
